import request from "@/utils/request"
import { getToken } from "@/utils/token"

// 登录
export const login=({mobile,code})=>request({
    url:'/v1_0/authorizations',
    method:'post',
    data:{
        mobile,
        code
    }
})

// 获取所有频道
export const getAllChannels=()=>request({
    url:'/v1_0/channels'
})

// 获取用户频道
export const getUserChannels=()=>request({
    url:'/v1_0/user/channels',
}) 

// 获取文章
export const getArticleList=({channel_id,timestamp})=>request({
    url:'/v1_0/articles',
    params:{
        channel_id,
        timestamp
    }
}) 
// 更新频道
export const updateChannel=(channels)=>request({
    url:'/v1_0/user/channels',
    method:'put',
    data:{
        channels
    }
})
// 删除用户频道
export const deleteUserChannel=(channelId)=>request({
    url:`/v1_0/user/channels/${channelId}`,
    method:'delete',
})
// 不感兴趣
export const disLikeArticle=(artId)=>request({
    url:'/v1_0/article/dislikes',
    method:'post',
    data:{
        target:artId
    }
})
// 反馈
export const reportArticle=(artId,type)=>request({
    url:'/v1_0/article/reports',
    method:'post',
    data:{
        target:artId,
        type:type,
        remark:'接口无'
    }
})
// 搜索联想
export const searchSuggestion=(keywords)=>request({
    url:'/v1_0/suggestion',
    params:{
        q:keywords
    }
})
// 获取搜索结果
export const searchResult=({page=1,per_page=10,q})=>request({
    url:'/v1_0/search',
    params:{
        page,
        per_page,
        q
    }
})

// 获取文章详情
export const articleDetail=(art_id)=>request({
    url:`/v1_0/articles/${art_id}`
})

// 关注
export const followApi=(user_id)=>request({
    url:'/v1_0/user/followings',
    method:'post',
    data:{
        target:user_id
    }
})
// 取消关注
export const cancelFollowApi=(user_id)=>request({
    url:`/v1_0/user/followings/${user_id}`,
    method:'delete'
})

// 文章点赞
export const artLike=(art_id)=>request({
    url:'/v1_0/article/likings',
    method:'post',
    data:{
        target:art_id
    }
})

// 取消文章点赞
export const artdDisLike=(art_id)=>request({
    url:`/v1_0/article/likings/${art_id}`,
    method:'delete',
})

// 获取评论
export const getComment=({id,offset=null,limit=10})=>request({
    url:'/v1_0/comments',
    params:{
        // axios只会针对params参数，如果值没传，则会忽略此参数和值不携带在url?后面 
        type:'a',
        source:id,
        offset,
        limit
    }
})

// 对文章评论-喜欢
export const commentLiking=(id)=>request({
    url:'/v1_0/comment/likings',
    method:'post',
    data:{
        target:id
    }
})

// 对文章评论-取消喜欢
export const commentDisLiking=(id)=>request({
    url:`/v1_0/comment/likings/${id}`,
    method:'delete'
})

// 发布评论
export const sendComment=(id,content,art_id=null)=>{
    // 判断第三个参数是否有值
    const obj={
        target:id,
        content
    }
    if(art_id!==null){
        obj.art_id=art_id
    }
    return request({
        url:'/v1_0/comments',
        method:'post',
        data:obj
    })
    
}

// 获取用户个人资料
export const getUserInfo=()=>request({
    url:'/v1_0/user/profile'
})

// 获取用户自己信息
export const getUserBaseInfo=()=>request({
    url:'/v1_0/user'
})

// 更新个人头像
export const updateAvatar=(fd)=>request({
    url:'/v1_0/user/photo',
    method:'patch',
    // 这里参数是一个文件类型
    data:fd
    // Content-Type:application.json;axios携带的，data请求题体是对象，json是字符串
    // Content-Type:multipart/form-data浏览器携带的，前提data请求体必须是FormData类型对象
})

// 更新用户名称
export const updateUserNickname=(dataObj)=>{
    // axios如果请求体是params，里面参数可自动忽略，请求体是data，里面参数会携带发送给后台，要做判断
    const obj={
        name:'',
        gender:0,
        birthday:'',
        intro:''
    }
    for(let k in obj){
        if(dataObj[k]===undefined){
            delete obj[k]
        }else{
            obj[k]=dataObj[k]
        }
    }
    return request({
        url:'/v1_0/user/profile',
        method:'patch',
        data:obj
    })
}


    